package avl;

public class AVLTreeDemo {
    public static void main(String[] args) {
        int[] arr = {10,9,12,11,13,7,8};
        AVLTree avlTree = new AVLTree();
        for (int i = 0; i < arr.length; i++) {
            avlTree.add(new Node(arr[i]));
        }
        avlTree.infixOrder();
        System.out.println("avlTree.getRoot().leftHeight() = " + avlTree.getRoot().leftHeight());
        System.out.println("avlTree.getRoot().rightHeight() = " + avlTree.getRoot().rightHeight());
        System.out.println("avlTree.getRoot().value = " + avlTree.getRoot().value);


    }
}

class AVLTree {
    private Node root;

    public Node getRoot() {
        return root;
    }

    /**
     * 删除节点的最小节点
     */
    public int deleteTreeMin(Node node) {
        if (node == null) {
            return -1;
        }
        Node target = node;
        while (target.left != null) {
            target = target.left;
        }
        deleteNode(target.value);
        return target.value;
    }

    /**
     * 删除节点
     *
     * @param value
     */
    public void deleteNode(int value) {
        if (root == null) {
            return;
        }
        Node target = search(value);
        if (target == null) {
            return;
        }
        //父节点可能为null
        if (root.left == null && root.right == null) {
            //说明只有root一个节点
            root = null;
            return;
        }
        //找到target的父节点
        Node parent = searchParent(value);
        //判断要删除节点是不是叶子节点
        if (target.left == null && target.right == null) {
            if (parent.left == target) {
                parent.left = null;
            } else {
                parent.right = null;
            }
        } else if (target.left != null && target.right != null) {//删除的节点有两个子节点
            int minValue = deleteTreeMin(target.right);
            if (minValue == -1) {
                return;
            }
            target.value = minValue;

        } else {//删除的节点只有一个子节点
            if (parent != null) {
                if (target == parent.left) {
                    if (target.left != null) {
                        parent.left = target.left;
                    } else {
                        parent.left = target.right;
                    }
                } else {
                    if (target.left != null) {
                        parent.right = target.left;
                    } else {
                        parent.right = target.right;
                    }
                }
            } else {
                if (target.left != null) {
                    root = target.left;
                } else {
                    root = target.right;
                }
            }

        }
    }


    public void add(Node node) {
        if (root == null) {
            root = node;
        } else {
            root.add(node);
        }
    }

    public Node search(int value) {
        if (root == null) {
            return null;
        } else {
            return root.search(value);
        }
    }

    public Node searchParent(int value) {
        if (root == null) {
            return null;
        } else {
            return root.searchParent(value);
        }
    }

    public void infixOrder() {
        if (root == null) {
            System.out.println("二叉树为空");
        } else {
            root.infixOrder();
        }
    }
}

class Node {
    int value;
    Node left;
    Node right;

    public Node(int value) {
        this.value = value;
    }

    /**
     * 求当前节点的左子树的高度
     *
     * @return
     */
    public int leftHeight() {
        return left == null ? 0 : left.height();
    }

    /**
     * 求当前节点的右子树的高度
     *
     * @return
     */
    public int rightHeight() {
        return right == null ? 0 : right.height();
    }

    /**
     * 求当前节点的高度
     *
     * @return
     */
    public int height() {
        return Math.max(left == null ? 0 : left.height(), right == null ? 0 : right.height()) + 1;
    }

    /**
     * 左旋转
     */
    public void leftRetort() {
        //创建新节点
        Node newNode = new Node(value);
        //新节点的左子树为当前节点的左子树
        newNode.left = left;
        //新节点的右子树为当前节点的右子树的左子树
        newNode.right = right.left;
        //当前节点的值为右子树的值
        value = right.value;
        //当前节点的右子树为右子树的右子树
        right = right.right;
        //当前节点的左子树为新节点
        left = newNode;
    }

    /**
     * 右旋转
     */
    public void rightRetort() {
        //创建新节点
        Node newNode = new Node(value);
        //新节点的右子树为当前节点的右子树
        newNode.right = right;
        //新节点的左子树为当前节点的左子树的右子树
        newNode.left = left.right;
        //当前节点的值为左子树的值
        value = left.value;
        //当前节点的左子树为左子树的左子树
        left = left.left;
        //当前节点的右子树为新节点
        right = newNode;
    }

    /**
     * 查找节点
     *
     * @param value
     * @return
     */
    public Node search(int value) {
        if (value == this.value) {
            return this;
        } else if (value < this.value) {
            if (this.left == null) {
                return null;
            }
            return this.left.search(value);
        } else {
            if (this.right == null) {
                return null;
            }
            return this.right.search(value);
        }
    }

    /**
     * 查找要找的节点的父节点
     *
     * @param value
     * @return
     */
    public Node searchParent(int value) {
        if ((this.left != null && this.left.value == value) ||
                (this.right != null && this.right.value == value)) {
            return this;
        } else {
            if (value < this.value && this.left != null) {
                return this.left.searchParent(value);
            } else if (value >= this.value && this.right != null) {
                return this.right.searchParent(value);
            } else {
                return null;
            }
        }
    }

    public void infixOrder() {
        if (this.left != null) {
            this.left.infixOrder();
        }
        System.out.println(this);
        if (this.right != null) {
            this.right.infixOrder();
        }
    }

    public void add(Node node) {
        if (node == null) {
            return;
        }
        if (node.value < this.value) {
            if (this.left == null) {
                this.left = node;
            } else {
                this.left.add(node);
            }
        } else {
            if (this.right == null) {
                this.right = node;
            } else {
                this.right.add(node);
            }
        }
        //判断是否需要旋转
        //如果右子树的高度大于左子树的高度
        if (rightHeight() - leftHeight() > 1) {
            if (right.rightHeight() > right.leftHeight()) {//RR形状
                //如果右子树的右子树高度大于右子树的左子树高度
                leftRetort();
            } else {//RL形状
                right.rightRetort();
                leftRetort();
            }
            return;
        }
        if (leftHeight() - rightHeight() > 1) {
            if (left.leftHeight() > left.rightHeight()) {//LL形状
                //如果左子树的左子树高度大于左子树的右子树高度
                rightRetort();
            } else {//LR形状
                left.leftRetort();
                rightRetort();
            }
            return;
        }
    }

    @Override
    public String toString() {
        return "Node{" +
                "value=" + value +
                '}';
    }
}
